package application.gui.workerWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.table.TableModel;
import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.PersonData;
import application.LoginAndAdministrationModule.Worker;
import application.client.data.Client;

public class ClientTablePanel extends JPanel {
	public ClientTablePanel(WorkerHomePage parent, Communication db,
			PersonData p) {
		this.parent = parent;
		this.db = db;
		this.worker = new Worker(p);
		init();

	}

	private void init() {
		setSize(600, 480);
		setLayout(null);
		filterInfo = new JLabel("Filtr");
		filter = new JComboBox<String>(new String[]{"Nadawcy","Odbiorcy", "Wszyscy"});
		model = new TableModel(data, collumnNames);
		clientTable = new JTable(model);
		clientTable.getTableHeader().setReorderingAllowed(false);
		pane = new JScrollPane(clientTable);
		setListeners();
		initData(0);
		filter.setSelectedIndex(0);
		addComponent(clientTable.getTableHeader(), 30, 70, 500, 30);
		addComponent(pane, 30, 100, 500, 300);
		addComponent(filterInfo, 30, 30, 80, 30);
		addComponent(filter, 130, 30, 120, 30);
		
	}

	private void addComponent(JComponent comp, int x, int y, int width,
			int height) {
		add(comp);
		comp.setBounds(x, y, width, height);
	}

	private void initData(int choice) {
		
		LinkedList<String[]> list = new LinkedList<String[]>();
		data = null;
		ArrayList<Client> clients = new ArrayList<Client>();
		ArrayList<PersonData> users = new ArrayList<PersonData>();
		if (choice == 2){
			try {
				clients = db.getAllclients();
				users = worker.show_users(1);
				for (Client c : clients) {
					//Imię i nazwisko", "Firma", "Email","Telefon", "Adres", "Kod pocztowy", "Miasto"
					list.add(new String[]{c.getFirstName()+" "+c.getLastName(),c.getCompany(),c.getEmail(),c.getPhoneNumber(),c.getAddress().getStreet()+" "+c.getAddress().getFlatNumber()+"/"+c.getAddress().getHouseNumber(),c.getAddress().getPostCode(),c.getAddress().getCity()});
				}
				for (PersonData c : users) {
					//Imię i nazwisko", "Firma", "Email","Telefon", "Adres", "Kod pocztowy", "Miasto"
					list.add(new String[]{c.getName()+" "+c.getSurname(),"",c.geteMail(),c.getephoneNumber(),c.getAddress().getStreet()+" "+c.getAddress().getFlatNumber()+"/"+c.getAddress().getHouseNumber(),c.getAddress().getPostCode(),c.getAddress().getCity()});
				}
				data = list.toArray(new String[7][list.size()]);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (choice == 1) {
			
			try {
				clients = db.getAllclients();
				for (Client c : clients) {
					//Imię i nazwisko", "Firma", "Email","Telefon", "Adres", "Kod pocztowy", "Miasto"
					list.add(new String[]{c.getFirstName()+" "+c.getLastName(),c.getCompany(),c.getEmail(),c.getPhoneNumber(),c.getAddress().getStreet()+" "+c.getAddress().getFlatNumber()+"/"+c.getAddress().getHouseNumber(),c.getAddress().getPostCode(),c.getAddress().getCity()});
				}
				data = list.toArray(new String[7][list.size()]);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if(choice==0){
			
			try {
				users = worker.show_users(1);
				for (PersonData c : users) {
					//Imię i nazwisko", "Firma", "Email","Telefon", "Adres", "Kod pocztowy", "Miasto"
					list.add(new String[]{c.getName()+" "+c.getSurname(),"",c.geteMail(),c.getephoneNumber(),c.getAddress().getStreet()+" "+c.getAddress().getFlatNumber()+"/"+c.getAddress().getHouseNumber(),c.getAddress().getPostCode(),c.getAddress().getCity()});
				}
				data = list.toArray(new String[7][list.size()]);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		remove(pane);
		remove(clientTable.getTableHeader());
		repaint();
		validate();
		model = new TableModel(data, collumnNames);
		clientTable = new JTable(model);
		clientTable.getTableHeader().setReorderingAllowed(false);
		pane = new JScrollPane(clientTable);
		addComponent(clientTable.getTableHeader(), 30, 70, 500, 30);
		addComponent(pane, 30, 100, 500, 300);
		if (list.size() == 0) {
			remove(pane);
		}
		repaint();
		validate();
	}
	
	private void setListeners(){
		filter.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				initData(filter.getSelectedIndex());
			}
		});
		
		
	}
	
	
	private JScrollPane pane;
	private JLabel filterInfo;
	private JComboBox<String> filter;
	private Worker worker;
	private TableModel model;
	private JTable clientTable;
	private String[] collumnNames = { "Imię i nazwisko", "Firma", "Email",
			"Telefon", "Adres", "Kod pocztowy", "Miasto" };
	private String[][] data;
	private WorkerHomePage parent;
	private Communication db;
	private JPanel tablePanel;
}
